import json
from django.db import models


class MainMenu(models.Model):
    main_menu_id = models.IntegerField(verbose_name='菜单编号')
    main_menu_name = models.CharField(max_length=255, verbose_name='名称')
    main_menu_url = models.CharField(max_length=255, blank=True, null=True)

    #  在这里我们手动序列化
    def __str__(self):
        result = {}
        result['main_menu_id'] = self.main_menu_id
        result['main_menu_name'] = self.main_menu_name
        # result['main_menu_url'] = self.main_menu_url
        return json.dumps(result, ensure_ascii=False)

    class Meta:
        managed = False
        db_table = 'main_menu'

        verbose_name = '主菜单'
        verbose_name_plural = verbose_name


class SubMenu(models.Model):
    main_menu_id = models.IntegerField(blank=True, null=True, verbose_name='菜单编号')
    sub_menu_id = models.IntegerField(blank=True, null=True, verbose_name='编号', )

    types = (
        ('channel', '一级分类'),
        ('dt', '二级分类'),
        ('dd', '三级分类'),
    )

    sub_menu_type = models.CharField(max_length=255, blank=True, null=True, choices=types, verbose_name='类型')
    sub_menu_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='名称')
    sub_menu_url = models.CharField(max_length=255, blank=True, null=True)
    # menu = models.ForeignKey(to=MainMenu, verbose_name='二级菜单', on_delete=models.CASCADE)

    def __str__(self):
        result = {}
        result['main_menu_id'] = self.main_menu_id
        result['sub_menu_id'] = self.sub_menu_id
        result['sub_menu_type'] = self.sub_menu_type
        result['sub_menu_name'] = self.sub_menu_name
        return json.dumps(result, ensure_ascii=False)

    def show(self):
        return self.main_menu_id

    # 把显示的方法名称改写成中文
    show.short_description = '对应的主菜单编号'

    # 指定方法中的数据默认按照模型类的那个字段排序
    show.admin_order_field = 'create_time'

    class Meta:
        managed = False
        db_table = 'sub_menu'

        verbose_name = '副级菜单'
        verbose_name_plural = verbose_name


# 后台的模块美化
class Status(models.TextChoices):
    UNSTARTED = 'u', "Not started yet"
    ONGOING = 'o', "Ongoing"
    FINISHED = 'f', "Finished"
